import { Component, OnInit } from '@angular/core';
import { StorageService } from '../services/storage.service';

// var storage = new StorageService();

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss']
})
export class SearchComponent implements OnInit {

  public keyWord:  any ;
  public historyList:any[]=[];// 必须有默认值

  constructor(public storage:StorageService) {
  
   }

  ngOnInit() {
      console.log('刷新会触发本生命周期函数');
      var searchlist = this.storage.get("searchlist");
      if(searchlist){
        this.historyList = searchlist;
      }
  }

  doSearch(){
    if(this.historyList.indexOf(this.keyWord) == -1){
        this.historyList.push(this.keyWord);
        this.keyWord='';

        //放入缓存中
        this.storage.set('searchlist',this.historyList);
    }
  }

  doDelete(key){
    this.historyList.splice(key,1);
      //放入缓存中
      this.storage.set('searchlist',this.historyList);
  }
}
